#!/usr/bin/env ruby 

require "atom/service"

require "rubygems"
require "rexml/document"

class Person
  @@service = Atom::Service.new "http://littleblackbookdb.cloudapp.net/littleblackbookservice.svc/"
  #@@service = Atom::Service.new "http://localhost:57965/littleblackbookservice.svc/"
  @@name = 'people'

  @@collection = @@service.collections.find { |c| c.title.to_s == @@name }
  @@feed = @@collection.feed

  attr_accessor :id, :fname, :lname, :phonenumber
  attr_accessor :atom_entry, :errors

  def self.find_all
    @@feed.update!

    people = Array.new
    @@feed.each do |entry|
      person = Person.new
      person.atom_entry = entry
      person.from_xml( entry.to_xml )
      people.push( person )
    end

    return( people )
  end

  def self.find( id ) 
    return self.find_all.find { |p| p.id == id }
  end

  def initialize( id = nil, fname = nil, lname = nil, phonenumber = nil )
    @id = id
    @fname = fname
    @lname = lname
    @phonenumber = phonenumber
    @errors = []
  end

  def from_xml( xml )
    # This is fairly fragile; A parser that understands
    # Microsoft's EDM packaging standard would be far
    # more robust.

    # As it is, this is just using canned XPath queries.

    @id = xml.get_elements( '//d:ID' ).first.text
    @fname = xml.get_elements( '//d:fname' ).first.text
    @lname = xml.get_elements( '//d:lname' ).first.text
    @phonenumber = xml.get_elements( '//d:phonenumber' ).first.text
  end

  def content_to_xml
    doc = REXML::Document.new
    prop = doc.add_element( 'properties' )

    prop.attributes[ 'xmlns:d' ] = "http://schemas.microsoft.com/ado/2007/08/dataservices" 
    prop.attributes[ 'xmlns' ] = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 

    el = prop.add_element( 'd:ID' )
    el.attributes[ 'type' ] = 'Edm.Int32'
    el.text = ( @id || 0 ).to_s

    el = prop.add_element( 'd:fname' )
    el.text = @fname

    el = prop.add_element( 'd:lname' )
    el.text = @lname
 
    el = prop.add_element( 'd:phonenumber' )
    el.text = @phonenumber

    return doc
  end

  def parse_params( params )
    if params[ :fname ] then
      @fname = params[ :fname ]
    end

    if params[ :lname ] then
      @lname = params[ :lname ]
    end

    if params[ :phonenumber ] then
      @phonenumber = params[ :phonenumber ]
    end
  end

  def update_attributes( params )
    parse_params( params )

    @atom_entry.content = content_to_xml
    @atom_entry.content[ 'type' ] = 'application/xml'
    @atom_entry.updated!

    res = @@collection.put! @atom_entry
  end

  def save
    @atom_entry = Atom::Entry.new
    @atom_entry.content = content_to_xml
    @atom_entry.content[ 'type' ] = 'application/xml'
    @atom_entry.updated!
 
    res = @@collection.post! @atom_entry

    @@feed.update!
    @atom_entry = @@feed.entries.last
    from_xml @atom_entry.content.to_xml 
  end

  def destroy
    res = @@collection.delete! @atom_entry
  end

  def to_s 
    return "#{@id}"
  end

  def name
    return "#{@fname} #{@lname}"
  end

  def bets
    return Bet.find_by_person( @id )
  end
end

